package com.pcbly.web.action.system;

import java.util.ArrayList;
import java.util.List;

import com.pcbly.web.action.BaseAction;
import com.pcbly.web.bean.table.TableBean;
import com.pcbly.web.bean.table.TableChildBean;
import com.pcbly.web.business.table.TableService;
import com.pcbly.web.components.utils.property.GlobalReadProperties;

public class TableAction extends BaseAction<TableBean> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8644207068476991265L;

	private TableService tableService;

	private TableBean tableBean;
	private TableChildBean tableChildBean;

	private List<TableBean> tableBeans;
	private List<TableChildBean> tableChildBeans;

	private String ids;

	public String index() throws Exception {
		long t = System.currentTimeMillis();
		if (GlobalReadProperties.getStringToProperty("table.lockTable")
				.indexOf("0") >= 0) {
			tableBeans = tableService.findTableBeansFromDatabase();
			List<TableBean> list = tableService.getTableBeans();
			List<TableBean> saveList = new ArrayList<TableBean>();
			for (TableBean childBean : tableBeans) {
				boolean iflag = false;
				if (!list.isEmpty()) {
					for (TableBean bean : list) {
						if (bean.getTableName()
								.equals(childBean.getTableName())) {
							iflag = true;
							break;
						}
					}
				}
				if (!iflag) {
					childBean.setIflag("1");
					saveList.add(childBean);
				}
			}
			if (!saveList.isEmpty())
				tableService.saveTableBeans(saveList);
		}
		tableChildBeans = tableService.findTableChildBeans();
		System.out.println("time ms::::::::::"
				+ (System.currentTimeMillis() - t));
		return super.index();
	}

	public String dwrCheck(String name) throws Exception {
		if (name == null || "".equals(name.trim())) {
			return "false";
		}
		boolean iflag = tableService.checkName(name);
		if (iflag) {
			return "true";
		} else {
			return "false";
		}
	}

	public List<TableBean> dwrIndex(String id) throws Exception {
		tableBeans = tableService.getTableBeans();
		TableBean bean = new TableBean();
		bean.setTableName("");
		bean.setId("0");
		tableBeans.set(0, bean);
		return tableBeans;
	}

	public String save() throws Exception {
		try {
			if (tableBean.getId() == null || "".equals(tableBean.getId())) {
				tableBean.setIflag("1");
				if (tableBean.getTableName() == null
						|| "".equals(tableBean.getTableName().trim())) {
					throw new Exception(
							"when create entity,the entity is null.");
				}
				if (tableBean.getTableName().trim().toLowerCase().indexOf("t_") >= 0) {
					tableBean.setTableNameExtention(tableBean.getTableName()
							.trim().concat("_ext"));
				} else {
					tableBean.setTableName("t_"
							.concat(tableBean.getTableName()));
					tableBean.setTableNameExtention("t_".concat(
							tableBean.getTableName()).concat("_ext"));
				}

				tableService.saveTableBean(tableBean);
				saveMessage(getText("entity.saved"));
				// saveMessage(getText("entity.saved",tableBean.getTableName()));
			} else {
				tableService.updateTableBean(tableBean);

				saveMessage(getText("entity.updated"));
			}
			return super.save();
		} catch (Exception e) {
			return ERROR;
		}
	}

	public String delete() throws Exception {
		tableService.deleteTableBean(getId());
		saveMessage(getText("entity.deleted"));
		return super.delete();
	}

	public String deletes() throws Exception {
		tableService.deleteTableBeansById(ids);
		saveMessage(getText("entity.deleted"));
		return super.delete();
	}

	public String view() throws Exception {
		tableBean = tableService.getTableBeanById(getId());
		return super.view();
	}

	public TableBean dwrView(String id) throws Exception {
		if (log.isDebugEnabled()) {
			log.debug("dwrView login.............");
			log.debug(id);
		}
		try {
			return tableService.getTableBeanById(id);
		} catch (Exception e) {
			log.error(e.getMessage());
			return null;
		}
	}

	public String dwrDelete(String id) throws Exception {
		try {
			tableService.deleteTableBeansById(id);
			return super.dwrBaseDelete(id);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			return ERROR;
		}
	}

	public String dwrSave(TableBean bean) throws Exception {
		try {
			if (bean.getId() == null || "".equals(bean.getId())) {
				if (bean.getTableName() == null
						|| "".equals(bean.getTableName().trim())) {
					throw new Exception(
							"when create entity,the entity is null.");
				}
				if (bean.getTableName().trim().toLowerCase().indexOf("t_") >= 0) {
					bean.setTableNameExtention(bean.getTableName().trim()
							.concat("_ext"));
				} else {
					bean.setTableName("t_".concat(bean.getTableName()));
					bean.setTableNameExtention(bean.getTableName().concat(
							"_ext"));
				}
				tableService.saveTableBean(bean);
			} else {
				if (bean.getTableName() == null
						|| "".equals(bean.getTableName().trim())) {
					throw new Exception(
							"when create entity,the entity is null.");
				}
				if (bean.getTableName().trim().toLowerCase().indexOf("t_") >= 0) {
					bean.setTableNameExtention(bean.getTableName().trim()
							.concat("_ext"));
				}else {
					bean.setTableName("t_".concat(bean.getTableName()));
					bean.setTableNameExtention(bean.getTableName().concat(
							"_ext"));
				}
				tableService.updateTableBean(bean);
			}
			return super.dwrBaseSave(bean.getId());
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
			return ERROR;
		}
	}

	public void setTableService(TableService tableService) {
		this.tableService = tableService;
	}

	public TableBean getTableBean() {
		return tableBean;
	}

	public void setTableBean(TableBean tableBean) {
		this.tableBean = tableBean;
	}

	public List<TableBean> getTableBeans() {
		return tableBeans;
	}

	public void setTableBeans(List<TableBean> tableBeans) {
		this.tableBeans = tableBeans;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public TableChildBean getTableChildBean() {
		return tableChildBean;
	}

	public void setTableChildBean(TableChildBean tableChildBean) {
		this.tableChildBean = tableChildBean;
	}

	public List<TableChildBean> getTableChildBeans() {
		return tableChildBeans;
	}

	public void setTableChildBeans(List<TableChildBean> tableChildBeans) {
		this.tableChildBeans = tableChildBeans;
	}
}
